# -*- coding: utf-8 -*-


#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.




class DBObject:

	def __init__(self):
		self._things = {} #inheirited
		self._needToUpdate = []
		
		self.id = -1

	def __getattr__(self, name):

		try:
			item = self._things[name]
		except KeyError:
			raise AttributeError, name

		if not item:
			item = self._getItem(name)

		return item
		

	def _getItem(self, name):
		raise AttributeError, name

	def updateDB(self, name):
		raise Exception, "There is no updateDB function for this type"

	def insert(self):
		raise Exception, "There is no insert function for this type"
		
	def delete(self):
		raise Exception, "There is no delete function for this type"


	def __setattr__(self, name, value):

		# "base case" to prevent recursion
		if name == '_things':
			self.__dict__[name] = value
			return

		if name in self._things:
			self._things[name] = value
			self.updateDB(name)
		else:
			self.__dict__[name] = value		#if not in things then put in our dict


	def __eq__(self, other):
		if self.id==-1 or other.id==-1:
			return False
		if self.id==other.id:
			return True
		else:
			return False
	
	def __ne__(self, other):
		return not (self.__eq__(other))